package com.example.doubt.dialog.global;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;

/**
 *  log日志全局浮窗的Service
 */
public class LogGlobalService extends Service {

	public static boolean isStarted = false;
	private WindowManager windowManager;
	private WindowManager.LayoutParams layoutParams;
	private LogGlobalViewManager logViewManager;

	@Override
	public void onCreate() {
		super.onCreate();
		isStarted = true;
		windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
		layoutParams = new WindowManager.LayoutParams();
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
			layoutParams.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
		}else{
			layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
		}
		layoutParams.format = PixelFormat.RGBA_8888;
		layoutParams.gravity = Gravity.START | Gravity.TOP;
		layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		layoutParams.width = getResources().getDisplayMetrics().widthPixels;
		layoutParams.height = 300;
		layoutParams.x = 0;
		layoutParams.y = 300;
	}

	@Nullable
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		isStarted = false;
	}

	@Override
	public boolean stopService(Intent name) {
		windowManager.removeView(logViewManager.getLogView(getApplicationContext()));
		return super.stopService(name);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		showLogGlobalView();
		return super.onStartCommand(intent, flags, startId);
	}

	private void showLogGlobalView() {
		if(logViewManager==null){
			logViewManager = LogGlobalViewManager.getInstance();
		}
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
			if(Settings.canDrawOverlays(this)){
				View logView = logViewManager.getLogView(getApplicationContext());
				windowManager.addView(logView, layoutParams);
				logView.setOnTouchListener(new FloatingOnTouchListener());
			}
		}else{
			View logView = logViewManager.getLogView(getApplicationContext());
			windowManager.addView(logView, layoutParams);
			logView.setOnTouchListener(new FloatingOnTouchListener());
		}
	}

	private class FloatingOnTouchListener implements View.OnTouchListener {
		private int x;
		private int y;

		@Override
		public boolean onTouch(View view, MotionEvent event) {
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					x = (int) event.getRawX();
					y = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_MOVE:
					int nowX = (int) event.getRawX();
					int nowY = (int) event.getRawY();
					int movedX = nowX - x;
					int movedY = nowY - y;
					x = nowX;
					y = nowY;
					layoutParams.x = layoutParams.x + movedX;
					layoutParams.y = layoutParams.y + movedY;
					windowManager.updateViewLayout(view, layoutParams);
					break;
				default:
					break;
			}
			return false;
		}
	}
}
